﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace TeF.Runner.UI
{
    [ValueConversion(typeof (IEnumerable<HierarchicalItemViewModel>), typeof (string))]
    public class SelectedItemsToLogConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            if (targetType != typeof (string))
                throw new ArgumentException("targetType");

            var selectedItems = value as IEnumerable<HierarchicalItemViewModel>;
            if (selectedItems == null)
                throw new ArgumentNullException("value");

            var logs =
                selectedItems
                    .Where(i => !string.IsNullOrWhiteSpace(i.Log))
                    .Select(i => string.Format("{1}{0}{2}{0}", Environment.NewLine, i.Name, i.Log));

            return string.Join(Environment.NewLine, logs);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
